package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
@Entity
@Table(name="driver")
public class Driver extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public String name; //姓名
	
	public String gender; //性别
	
	public String drive_licence_type; //驾照类型
	
	public Integer driving_age; //驾龄
	
	public Integer A_drivingage; //A照驾龄
	
	public String bussiness_licence; //营运证
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date work_date; //入职日期
	
	public Integer safe_drive_day; //安全行车
	
	public Integer vacation_record; //请假记录
	
	public Integer violation_record; //违规记录
	
	public Integer traffic_violation; //违章记录
	
	public Integer accident_record; //事故记录
	
	public String phone; //联系电话
	
	public String licence_num; //驾驶证号
	
	public String image; //相片
	public static Model.Finder<Integer, Driver> find = new Model.Finder<Integer, Driver>(Integer.class, Driver.class);

	public static Page<Driver> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Driver> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setName(String d) {
		name= d;
	}
	public String getName() {
		return name;
	}
	public void setGender(String d) {
		gender= d;
	}
	public String getGender() {
		return gender;
	}
	public void setDrive_licence_type(String d) {
		drive_licence_type= d;
	}
	public String getDrive_licence_type() {
		return drive_licence_type;
	}
	public void setDriving_age(Integer d) {
		driving_age= d;
	}
	public Integer getDriving_age() {
		return driving_age;
	}
	public void setA_drivingage(Integer d) {
		A_drivingage= d;
	}
	public Integer getA_drivingage() {
		return A_drivingage;
	}
	public void setBussiness_licence(String d) {
		bussiness_licence= d;
	}
	public String getBussiness_licence() {
		return bussiness_licence;
	}
	public void setWork_date(Date d) {
		work_date= d;
	}
	public Date getWork_date() {
		return work_date;
	}
	public void setSafe_drive_day(Integer d) {
		safe_drive_day= d;
	}
	public Integer getSafe_drive_day() {
		return safe_drive_day;
	}
	public void setVacation_record(Integer d) {
		vacation_record= d;
	}
	public Integer getVacation_record() {
		return vacation_record;
	}
	public void setViolation_record(Integer d) {
		violation_record= d;
	}
	public Integer getViolation_record() {
		return violation_record;
	}
	public void setTraffic_violation(Integer d) {
		traffic_violation= d;
	}
	public Integer getTraffic_violation() {
		return traffic_violation;
	}
	public void setAccident_record(Integer d) {
		accident_record= d;
	}
	public Integer getAccident_record() {
		return accident_record;
	}
	public void setPhone(String d) {
		phone= d;
	}
	public String getPhone() {
		return phone;
	}
	public void setLicence_num(String d) {
		licence_num= d;
	}
	public String getLicence_num() {
		return licence_num;
	}
	public void setImage(String d) {
		image= d;
	}
	public String getImage() {
		return image;
	}
	public static Driver findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Driver findByName(String d) {
		return find.where().eq("name", d).findUnique();
	}
	public static String getNameJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getName().toString(), v.getName().toString()); 
		}
		return node.toString();
	}
	public static Driver findByGender(String d) {
		return find.where().eq("gender", d).findUnique();
	}
	public static String getGenderJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getGender().toString(), v.getGender().toString()); 
		}
		return node.toString();
	}
	public static Driver findByDrive_licence_type(String d) {
		return find.where().eq("drive_licence_type", d).findUnique();
	}
	public static String getDrive_licence_typeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getDrive_licence_type().toString(), v.getDrive_licence_type().toString()); 
		}
		return node.toString();
	}
	public static Driver findByDriving_age(Integer d) {
		return find.where().eq("driving_age", d).findUnique();
	}
	public static String getDriving_ageJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getDriving_age().toString(), v.getDriving_age().toString()); 
		}
		return node.toString();
	}
	public static Driver findByA_drivingage(Integer d) {
		return find.where().eq("A_drivingage", d).findUnique();
	}
	public static String getA_drivingageJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getA_drivingage().toString(), v.getA_drivingage().toString()); 
		}
		return node.toString();
	}
	public static Driver findByBussiness_licence(String d) {
		return find.where().eq("bussiness_licence", d).findUnique();
	}
	public static String getBussiness_licenceJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getBussiness_licence().toString(), v.getBussiness_licence().toString()); 
		}
		return node.toString();
	}
	public static Driver findByWork_date(Date d) {
		return find.where().eq("work_date", d).findUnique();
	}
	public static String getWork_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getWork_date().toString(), v.getWork_date().toString()); 
		}
		return node.toString();
	}
	public static Driver findBySafe_drive_day(Integer d) {
		return find.where().eq("safe_drive_day", d).findUnique();
	}
	public static String getSafe_drive_dayJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getSafe_drive_day().toString(), v.getSafe_drive_day().toString()); 
		}
		return node.toString();
	}
	public static Driver findByVacation_record(Integer d) {
		return find.where().eq("vacation_record", d).findUnique();
	}
	public static String getVacation_recordJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getVacation_record().toString(), v.getVacation_record().toString()); 
		}
		return node.toString();
	}
	public static Driver findByViolation_record(Integer d) {
		return find.where().eq("violation_record", d).findUnique();
	}
	public static String getViolation_recordJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getViolation_record().toString(), v.getViolation_record().toString()); 
		}
		return node.toString();
	}
	public static Driver findByTraffic_violation(Integer d) {
		return find.where().eq("traffic_violation", d).findUnique();
	}
	public static String getTraffic_violationJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getTraffic_violation().toString(), v.getTraffic_violation().toString()); 
		}
		return node.toString();
	}
	public static Driver findByAccident_record(Integer d) {
		return find.where().eq("accident_record", d).findUnique();
	}
	public static String getAccident_recordJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getAccident_record().toString(), v.getAccident_record().toString()); 
		}
		return node.toString();
	}
	public static Driver findByPhone(String d) {
		return find.where().eq("phone", d).findUnique();
	}
	public static String getPhoneJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getPhone().toString(), v.getPhone().toString()); 
		}
		return node.toString();
	}
	public static Driver findByLicence_num(String d) {
		return find.where().eq("licence_num", d).findUnique();
	}
	public static String getLicence_numJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Driver v: find.all()) {
			node.put(v.getLicence_num().toString(), v.getLicence_num().toString()); 
		}
		return node.toString();
	}
}
